#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <dicom/queue.h>

queue_t *packet_queue;

int main (int argc, char *argv[])
{
    pthread_t thread_packet_receiver;
    pthread_t thread_command_handler;
    pthread_attr_t attr;
    int rc;
    void *status;

    packet_queue = queue_create ();
    if (packet_queue == NULL) {
        printf ("Error while creating packet queue.\n");
        exit (-1);
    }

    pthread_attr_init (&attr);
    pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE);

    rc = pthread_create (&thread_packet_receiver, &attr, gvcp_packet_receiver, packet_queue);
    if (rc) {
        printf ("Unable to create packet receiver thread. (code = %d)\n", rc);
        exit (-1);
    }

    rc = pthread_create (&thread_command_handler, &attr, gvcp_packet_handler, packet_queue);
    if (rc) {
        printf ("Unable to create command handler thread. (code = %d)\n", rc);
        exit (-1);
    }

    pthread_attr_destroy (&attr);

    rc = pthread_join (&thread_packet_receiver, &status);
    if (rc) {
        printf ("Error while joining packet receiver thread. (code = %d)\n", rc);
        exit (-1);
    }

    rc = pthread_join (&thread_command_handler, &status);
    if (rc) {
        printf ("Error while joining command handler thread. (code = %d)\n", rc);
        exit (-1);
    }

    pthread_exit (NULL);
    queue_destroy (packet_queue);

    return 0;
}

